package com.xin.components.base
{
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

/**
 * 滚动条基类
 * @author daichangxin
 * 
 */
public class ScrollBarBase extends LiteSprite
{
	/**
	 * 滚动条
	 */
	protected var _bar:Sprite;
	/**
	 * 滚动槽
	 */
	protected var _track:InteractiveObject;
	/**
	 * 左按钮/上按钮
	 */
	protected var _leftButton:InteractiveObject;
	/**
	 * 右按钮/下按钮
	 */
	protected var _rightButton:InteractiveObject;
	
	protected var _value:Number = 0;
	protected var _max:Number = 100;
	protected var _min:Number = 0;
	protected var _tick:Number = 1;
	protected var _lineSize:Number = 15;
	protected var _pageSize:Number = 50;
	protected var _barPercent:Number = 1;
	
	protected var _trackClickAble:Boolean = true;
	
	public function ScrollBarBase(w:int, h:int, bar:Sprite, track:InteractiveObject, leftButton:InteractiveObject, rightButton:InteractiveObject)
	{
		_bar = bar;
		_track = track;
		_leftButton = leftButton;
		_rightButton = rightButton;
		
		super(w, h);
	}
	
	protected override function addChildren():void
	{
		addChild(_track);
		addChild(_bar);
		addChild(_leftButton);
		addChild(_rightButton);
		
		_leftButton.addEventListener(MouseEvent.CLICK, onLeftButtonClick);
		_rightButton.addEventListener(MouseEvent.CLICK, onRightButtonClick);
		_bar.addEventListener(MouseEvent.MOUSE_DOWN, onBarMouseDown);
	}
	
	///////////////////////////////////////////////需要子类覆盖重写
	/**
	 * 初始化元素大小和默认位置（bar的位置由updateBarPosionByScrollValue()方法单独设置）
	 */	
	protected function initElementsSizeAndPositon():void
	{
		
	}
	
	/**
	 * 返回bar拖动范围，
	 * 需要子类继承实现
	 */	
	protected function getBarDragRange():Rectangle
	{
		return null;
	}
	
	/**
	 * 根据bar当前位置计算滚动值，
	 * 需要子类实现
	 */	
	protected function countScrollValueByBarPosition():Number
	{
		return 0;
	}
	
	/**
	 * 根据滚动值设置bar的位置,
	 * 需要子类实现
	 */	
	protected function updateBarPosionByScrollValue():void
	{
		
	}
	
	///////////////////////////////////////////////
	
	public override function draw():void
	{
		initElementsSizeAndPositon();
		
		//轨道接受点击
		if (_trackClickAble)
		{
			_track.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown_track);
		}
		else
		{
			_track.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown_track);
		}
		
		updateBarPosionByScrollValue();
	}
	
	/**
	 * 根据最大／最小滚动值纠正value数据
	 */	
	protected function correctValue():void
	{
		if (_max > _min)
		{
			_value = Math.min(_value, _max);
			_value = Math.max(_min, _value);
		}
		else
		{
			_value = Math.min(_value, _min);
			_value = Math.max(_value, _max);
		}
	}
	
	/**
	 * 设置bar相比track的比例（用于设置被拖动内容与滚动条大小尺寸的 对应关系）
	 * <p>最大为1
	 */	
	public function setBarPercent(v:Number):void
	{
		_barPercent = Math.min(v, 1);
		invalidate();
	}
	
	/**
	 * 设置滚动条数值
	 */	
	public function setValue(min:Number, max:Number, value:Number):void
	{
		_min = min;
		_max = max;
		_value = value;
		
		correctValue();
		updateBarPosionByScrollValue();
	}
	
	/**
	 * 鼠标在bar上按下
	 */	
	protected function onBarMouseDown(e:MouseEvent):void
	{
		_bar.startDrag(false, getBarDragRange());
		
		stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove_stage);
		stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp_stage);
	}
	
	/**
	 * 按住bar，且鼠标在舞台上移动,根据bar的位置计算当前滚动值
	 */	
	protected function onMouseMove_stage(e:MouseEvent):void
	{
		var oldValue:Number = _value;
		
		_value = countScrollValueByBarPosition();
		if (_value != oldValue)
			dispatchEvent(new Event(Event.CHANGE));
	}
	
	/**
	 * 当鼠标从舞台上弹起
	 */	
	protected function onMouseUp_stage(e:MouseEvent):void
	{
		stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp_stage);
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove_stage);
		
		_bar.stopDrag();
	}
	
	/**
	 * 鼠标在track上按下
	 */	
	protected function onMouseDown_track(e:MouseEvent):void
	{
		
	}
	
	/**
	 * 点击左按钮
	 */	
	protected function onLeftButtonClick(e:MouseEvent):void
	{
		this.value -= _lineSize;
	}
	
	/**
	 * 点击右按钮
	 */	
	protected function onRightButtonClick(e:MouseEvent):void
	{
		this.value += _lineSize;
	}
	
	/**
	 * 点击track每次滚动的像素值
	 */	
	public function set pageSize(value:Number):void
	{
		_pageSize = value;
	}
	public function get pageSize():Number
	{
		return _pageSize;
	}
	
	/**
	 * 返回当前bar相对于track的比例
	 */	
	public function get barPercent():Number
	{
		return _barPercent;
	}
	
	/**
	 * 返回真实滚动值（包含各种小数点）
	 */	
	protected function get trueValue():Number
	{
		return _value;
	}
	
	/**
	 * 设置当前滚动值
	 */	
	public function set value(v:Number):void
	{
		if (v == _value) return;
		
		_value = v;
		
		correctValue();
		updateBarPosionByScrollValue();
		dispatchEvent(new Event(Event.CHANGE));
	}
	/**
	 * 当前滚动值(经过最小单位tick处理)
	 */	
	public function get value():Number
	{
		return Math.round(_value / _tick) * _tick;
	}
	
	/**
	 * 滚动最小值
	 */	
	public function set minimum(v:Number):void
	{
		_min = v;
		
		correctValue();
		updateBarPosionByScrollValue();
	}
	public function get minimum():Number
	{
		return _min;
	}
	
	/**
	 * 滚动最大值
	 */	
	public function set maximum(v:Number):void
	{
		_max = v;
		
		correctValue();
		updateBarPosionByScrollValue();
	}
	public function get maximum():Number
	{
		return _max;
	}
	
	/**
	 * 滚动值最小单位
	 */	
	public function set tick(v:Number):void
	{
		_tick = v;
	}
	public function get tick():Number
	{
		return _tick;
	}
	
	/**
	 * 滚动轨道track是否接受点击
	 */	
	public function set trackClickAble(value:Boolean):void
	{
		_trackClickAble = value;
		invalidate();
	}
	public function get trackClickAble():Boolean
	{
		return _trackClickAble;
	}
	
	/**
	 * 按下左／右按钮时每次滚动的间隔像素
	 */	
	public function set lineSize(v:Number):void
	{
		_lineSize = v;
	}
	public function get lineSize():Number
	{
		return _lineSize;
	}
}
}